using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace ZuDoKu.Controls
{
    public class RadialMenu : Control
    {
        public double Radius { get; set; }

        public RadialMenu()
        {
            Radius = 15;
            IsContainer = true;
        }

        public void Add(RadialMenuItem item)
        {
            if (Focused == null)
                Focused = item;

            Controls.Add(item);
            RefreshLayout();
        }

        public void RefreshLayout()
        {
            int count = Controls.Count;
            double theta = (2 * Math.PI) / count;

            for (int n = 0; n < count; n++)
            {
                RadialMenuItem item = Controls[n] as RadialMenuItem;
                double itemTheta = theta * (n + 1);

                int y = (int)(Radius * Math.Sin(itemTheta)) * -1;
                int x = (int)(Radius * Math.Cos(itemTheta));

                item.X = x - (item.Width / 2);
                item.Y = y - (item.Height / 2);

                (item as IRadialItem).Theta = itemTheta;
                (item as IRadialItem).Range = theta * 2;
                (item as IRadialItem).PerformLayout();
            }
        }

        GamePadState _last;
        protected override void ProcessInput(GamePadState state)
        {
            Vector2 stick = state.ThumbSticks.Left;

            if (stick.X != 0 || stick.Y != 0)
            {
                double stickTheta = Math.Atan2(stick.Y, stick.X);
                if (stickTheta < 0)
                    stickTheta = (2 * Math.PI) - (stickTheta * -1);

                int count = Controls.Count;
                double theta = (2 * Math.PI) / count;

                for (int n = 0; n < count; n++)
                {
                    double itemTheta = theta * (n + 1);
                    double itemThetaZone = theta / 2d;

                    if((itemTheta - itemThetaZone < stickTheta &&
                        itemTheta + itemThetaZone > stickTheta) ||
                        (itemTheta + itemThetaZone > 2 * Math.PI &&
                        (itemTheta + itemThetaZone % (2 * Math.PI)) > stickTheta))
                    {
                        RadialMenuItem item = Controls[n] as RadialMenuItem;
                        Select(item);
                        break;
                    }
                }
            }            
            else if (_last.IsButtonUp(Buttons.DPadUp) && state.IsButtonDown(Buttons.DPadUp))
                SelectUp();
            else if (_last.IsButtonUp(Buttons.DPadDown) && state.IsButtonDown(Buttons.DPadDown))
                SelectDown();
            else if (_last.IsButtonUp(Buttons.DPadLeft) && state.IsButtonDown(Buttons.DPadLeft))
                SelectLeft();
            else if (_last.IsButtonUp(Buttons.DPadRight) && state.IsButtonDown(Buttons.DPadRight))
                SelectRight();

            _last = state;

            base.ProcessInput(state);
        }

        private void SelectRight()
        {
            //Select(settings);
        }

        private void SelectLeft()
        {
            //Select(social);
        }

        private void SelectDown()
        {
            //Select(cont);
        }

        private void SelectUp()
        {
            //Select(newGame);
        }

        private void Select(Button button)
        {
            if(Focused !=null)
                (Focused as Button).SelectionState = ButtonSelectionState.Normal;

            button.SelectionState = ButtonSelectionState.Selected;
            Focused = button;
        }
    }
}